package com.karl.mytask_2.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.karl.mytask_2.R;
import com.karl.mytask_2.activity.TaskInfoActivity;
import com.karl.mytask_2.bean.MyUser;
import com.karl.mytask_2.bean.Task;
import com.karl.mytask_2.tools.LogUtil;
import com.karl.mytask_2.tools.MyBaseFragment;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Karl on 2016/5/20.
 */
public class TaskFragment extends MyBaseFragment<Task> {
    @Override
    public void loadData() {
        if (mSwipeRefresh.isRefreshing()) {
            mAdapter.setHasMore(true);
            mList.clear();
        }
        int size = mList.size();
        getData(size);
    }
    private void getData(int size){
        MyUser user = BmobUser.getCurrentUser(getContext(),MyUser.class);
        //查询所有数据
        BmobQuery<Task> taskInfo = new BmobQuery<>();
        //只查询待接单的任务
        taskInfo.addWhereContains("receiver", user.getObjectId());
        taskInfo.setLimit(10);
        taskInfo.setSkip(size++ * 10);
        taskInfo.order("-createdAt");
        taskInfo.include("publisher");
        taskInfo.findObjects(getActivity(), new FindListener<Task>() {
            @Override
            public void onSuccess(List<Task> list) {
//                Toast.makeText(getContext(), "获取到数据" + list.size(), Toast.LENGTH_SHORT).show();
                mList.addAll(list);

                if (mList.size() > list.size()) {
                    mAdapter.setHasMore(false);
                }
                mAdapter.setIsLoading(false);
                mAdapter.notifyDataSetChanged();
                refreshList();
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(getActivity(), "查询失败！", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 1){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_header_task,null);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,null);
        }
        MyViewHolder mViewHolder = new MyViewHolder(view);
        return mViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (holder.getItemViewType() == 1){
            holder.mTaskCount.setText("已接受任务："+mList.size());
            return;
        }
        String mIconUrl = mList.get(position-1).getPublisher().getIcon();
        ImageOptions mImageOptions2 = new ImageOptions.Builder().setAutoRotate(true).setCircular(true)
                .setFadeIn(true).setSize(80,80).setLoadingDrawableId(R.mipmap.ic_launcher).build();
        if (mIconUrl == null){
            x.image().bind(holder.mIcon,
                    "http://cdnq.duitang.com/uploads/item/201412/27/20141227140012_BV2Bu.jpeg",
                    mImageOptions2);
        }else {

            x.image().bind(holder.mIcon, mIconUrl, mImageOptions2);
        }
        String publishName = mList.get(position-1).getPublisher().getNickName();
        switch (mList.get(position-1).getStatus()){
            case "1":
                holder.mName.setText(publishName+"-进行中");
                break;
            case "2":
                holder.mName.setText(publishName+"-待确认");
                break;
            case "3":
                holder.mName.setText(publishName+"-已结束");
                break;
            default:
                break;
        }

        holder.mTitle.setText(mList.get(position-1).getTitle().toString());
        holder.mPrice.setText(mList.get(position-1).getPrice().toString());

    }

    @Override
    public void onItemClick(int position, Task item, MyViewHolder holder) {
        //点击进入详情页面
        Intent intent = new Intent();
        LogUtil.d("这是#" + item.getTitle());
        intent.putExtra("id", mList.get(position-1).getObjectId());
        intent.putExtra("title",mList.get(position-1).getTitle());
        intent.putExtra("content",mList.get(position-1).getContent());
        intent.putExtra("address",mList.get(position-1).getAddress());
        intent.putExtra("price",mList.get(position-1).getPrice());
        intent.putExtra("status",mList.get(position-1).getStatus());
        intent.putExtra("publisher",mList.get(position-1).getPublisher().getObjectId());
        intent.putExtra("name",mList.get(position-1).getPublisher().getNickName());
        intent.putExtra("phone",mList.get(position-1).getPublisher().getMobilePhoneNumber());
        intent.putExtra("icon",mList.get(position-1).getPublisher().getIcon());
        startActivity(intent.setClass(getActivity(), TaskInfoActivity.class));
    }


    @Override
    public int getItemCount() {
        return super.getItemCount()+1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 ){
            return 1;//头布局
        }else if (position == mList.size()+1){
            return 3;
        }
        return super.getItemViewType(position);
    }

    @Override
    public void changeToolBarTxt() {
        mAppName.setText("任务列表");
    }
}
